package com.ynet.middleground.user.service.impl;

import java.time.LocalDateTime;
import java.util.List;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

import org.apache.dubbo.config.annotation.Service;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.base.Throwables;
import com.ynet.core.common.BaseReqObj;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.user.bean.*;
import com.ynet.middleground.user.common.ErrorMessageConstant;
import com.ynet.middleground.user.common.SystemConstant;
import com.ynet.middleground.user.dao.EsignLogMapper;
import com.ynet.middleground.user.dto.EsignAccountInfoDTO;
import com.ynet.middleground.user.dto.EsignValidDetailDTO;
import com.ynet.middleground.user.dto.IndividualFaceDTO;
import com.ynet.middleground.user.dto.SubbranchDto;
import com.ynet.middleground.user.entity.EsignLog;
import com.ynet.middleground.user.external.esign.ApiHelper;
import com.ynet.middleground.user.model.EsignApiModel;
import com.ynet.middleground.user.model.EsignModel;
import com.ynet.middleground.user.service.EsignService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author liulx
 * @description E签宝服务接口实现类
 * @date 2019-11-06 23:36
 */
@Api(value = "E签宝相关接口服务", tags = {"E签宝相关接口服务"})
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Path("esign")
@Service(timeout = 6000, version = "0.0.1")
public class EsignServiceImpl implements EsignService, GenericService {

    @Autowired
    private EsignModel esignModel;

    @Autowired
    private EsignApiModel esignApiModel;

    @Autowired
    private ApiHelper apiHelper;

    @Autowired
    private EsignLogMapper esignLogMapper;

    @Autowired
    private Mapper mapper;

    /**
     * 个人 2 要素比对
     *
     * @param req 个人 2 要素比对请求对象
     * @return 信息比对业务 id
     * @author liulx
     */
    @POST
    @Path("valid-base2")
    @ApiOperation(value = "个人2要素比对", notes = "result 返回为信息比对业务 Id，请注意保存该参数")
    @ApiImplicitParam(name = "requestObj", dataType = "ValidPersonBaseReq", paramType = "body", required = true)
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public ServiceResult<String> individualVerifyBase(ValidPersonBaseReq req) {
        ServiceResult<String> result = new ServiceResult<>();
        EsignLog esignLog = mapper.map(req, EsignLog.class);
        esignLog.setCreateBy(req.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_19);
        esignLog.setUserType(SystemConstant.ESIGN_USER_TYPE_PERSON);
        esignLog.setRequestMsg(req.toString());
        try {
            result.setResult(esignApiModel.individualVerify2Base(req));
            esignLog.setResponseMsg(result.getResult());
            esignLog.setAccountId(result.getResult());
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "E签宝个人 2 要素比对服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(result.getErrorCode() + result.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return result;
        }
        esignLogMapper.insert(esignLog);
        return result;
    }

    /**
     * 创建E签宝个人账户
     *
     * @param createEsignPersonReq 创建E签宝个人账户请求对象
     * @return 个人客户账户号 AccountId
     * @author liulx
     */
    @POST
    @ApiOperation(value = "创建E签宝个人账户", notes = "创建E签宝个人账户")
    @ApiImplicitParam(name = "requestObj", dataType = "CreateEsignPersonReq", paramType = "body", required = true)
    @Override
    public ServiceResult<String> addPersonAccount(CreateEsignPersonReq createEsignPersonReq) {
        ServiceResult<String> result = new ServiceResult<>();
        EsignLog esignLog = mapper.map(createEsignPersonReq, EsignLog.class);
        esignLog.setCreateBy(createEsignPersonReq.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_01);
        esignLog.setUserType(SystemConstant.ESIGN_USER_TYPE_PERSON);
        esignLog.setRequestMsg(createEsignPersonReq.toString());
        try {
            result.setResult(esignModel.addPersonAccount(createEsignPersonReq));
            esignLog.setResponseMsg(result.getResult());
            esignLog.setAccountId(result.getResult());
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "创建E签宝个人账户服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(result.getErrorCode() + result.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return result;
        }
        esignLogMapper.insert(esignLog);
        return result;
    }

    /**
     * 创建E签宝企业账户
     *
     * @param createEsignOrganizeReq 创建E签宝企业账户请求对象
     * @return 企业客户账户号 AccountId
     * @author liulx
     */
    @POST
    @Path("organize")
    @ApiOperation(value = "创建E签宝企业账户", notes = "创建E签宝企业账户")
    @ApiImplicitParam(name = "requestObj", dataType = "CreateEsignOrganizeReq", paramType = "body", required = true)
    @Override
    public ServiceResult<String> addOrganizeAccount(CreateEsignOrganizeReq createEsignOrganizeReq) {
        ServiceResult<String> result = new ServiceResult<>();
        EsignLog esignLog = mapper.map(createEsignOrganizeReq, EsignLog.class);
        esignLog.setCreateBy(createEsignOrganizeReq.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_00);
        esignLog.setUserType(SystemConstant.ESIGN_USER_TYPE_ORGANIZE);
        esignLog.setRegisterType(createEsignOrganizeReq.getUserType());
        esignLog.setRegType(createEsignOrganizeReq.getRegType());
        esignLog.setOrganType(String.valueOf(createEsignOrganizeReq.getOrganType()));
        esignLog.setRequestMsg(createEsignOrganizeReq.toString());
        try {
            result.setResult(esignModel.addOrganizeAccount(createEsignOrganizeReq));
            esignLog.setResponseMsg(result.getResult());
            esignLog.setAccountId(result.getResult());
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "创建E签宝企业账户服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(result.getErrorCode() + result.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return result;
        }
        esignLogMapper.insert(esignLog);
        return result;
    }

    /**
     * 根据证件号获取账户信息
     *
     * @param infoReq 查询E签宝账户信息请求对象
     * @return E签宝用户信息传输对象
     * @author liulx
     */
    @POST
    @Path("get-account-info-by-id-no")
    @ApiOperation(value = "根据证件号获取账户信息", notes = "根据证件号获取账户信息")
    @ApiImplicitParam(name = "requestObj", dataType = "QueryEsignAccountInfoReq", paramType = "body", required = true)
    @Override
    public ServiceResult<EsignAccountInfoDTO> getAccountInfoByIdNo(QueryEsignAccountInfoReq infoReq) {
        ServiceResult<EsignAccountInfoDTO> result = new ServiceResult<>();
        EsignLog esignLog = mapper.map(infoReq, EsignLog.class);
        esignLog.setCreateBy(infoReq.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_06);
        esignLog.setRequestMsg(infoReq.toString());
        try {
            result.setResult(esignModel.getAccountInfoByIdNo(infoReq));
            esignLog.setResponseMsg(result.getResult().toString());
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "创建E签宝企业账户服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(result.getErrorCode() + result.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return result;
        }
        esignLogMapper.insert(esignLog);
        return result;
    }

    /**
     * 更新E签宝个人账户信息
     *
     * @param req 更新E签宝个人账户请求对象
     * @return 更新结果以 success 字段值标识 true/false
     */
    @PUT
    @ApiOperation(value = "更新E签宝个人账户信息", notes = "更新E签宝个人账户信息")
    @ApiImplicitParam(name = "requestObj", dataType = "UpdateEsignPersonReq", paramType = "body", required = true)
    @Override
    public ServiceResult updatePersonAccount(UpdateEsignPersonReq req) {
        ServiceResult result = new ServiceResult<>();
        EsignLog esignLog = mapper.map(req, EsignLog.class);
        esignLog.setAccountId(req.getAccountUid());
        esignLog.setCreateBy(req.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_03);
        esignLog.setUserType(SystemConstant.ESIGN_USER_TYPE_PERSON);
        esignLog.setRequestMsg(req.toString());
        try {
            esignModel.updateAccount(req);
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "更新E签宝个人账户信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(result.getErrorCode() + result.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return result;
        }
        esignLogMapper.insert(esignLog);
        return result;
    }

    /**
     * 更新E签宝企业账户信息
     *
     * @param req 更新E签宝企业账户请求对象
     * @return 更新结果以 success 字段值标识 true/false
     */
    @PUT
    @Path("organize")
    @ApiOperation(value = "更新E签宝企业账户信息", notes = "更新E签宝企业账户信息")
    @ApiImplicitParam(name = "requestObj", dataType = "UpdateEsignOrganizeReq", paramType = "body", required = true)
    @Override
    public ServiceResult updateOrganizeAccount(UpdateEsignOrganizeReq req) {
        ServiceResult result = new ServiceResult<>();
        EsignLog esignLog = mapper.map(req, EsignLog.class);
        esignLog.setAccountId(req.getAccountUid());
        esignLog.setCreateBy(req.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_02);
        esignLog.setUserType(SystemConstant.ESIGN_USER_TYPE_ORGANIZE);
        esignLog.setRegisterType(req.getUserType());
        esignLog.setOrganType(req.getOrganType());
        esignLog.setRequestMsg(req.toString());
        try {
            esignModel.updateAccount(req);
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "更新E签宝个人账户信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(result.getErrorCode() + result.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return result;
        }
        esignLogMapper.insert(esignLog);
        return result;
    }

    /**
     * 注销E签宝账户
     *
     * @param req 注销E签宝账户请求对象
     * @return 注销结果以 success 字段值标识 true/false
     */
    @DELETE
    @ApiOperation(value = "注销E签宝账户", notes = "注销账户，注销后账户将不可再使用，请谨慎调用")
    @ApiImplicitParam(name = "requestObj", dataType = "DeleteEsignAccountReq", paramType = "body", required = true)
    @Override
    public ServiceResult deleteAccount(DeleteEsignAccountReq req) {
        ServiceResult result = new ServiceResult<>();
        EsignLog esignLog = mapper.map(req, EsignLog.class);
        esignLog.setAccountId(req.getAccountUid());
        esignLog.setCreateBy(req.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_04);
        esignLog.setRequestMsg(req.toString());
        try {
            esignModel.deleteAccount(req);
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "更新E签宝个人账户信息服务异常，case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(result.getErrorCode() + result.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return result;
        }
        esignLogMapper.insert(esignLog);
        return result;
    }

    @POST
    @Path("individual-face")
    @ApiOperation(value = "【人脸识别认证】个人核身", notes = "【人脸识别认证】个人核身（个人实名认证）")
    @ApiImplicitParam(name = "requestObj", dataType = "GetIndividualFaceReq", paramType = "body", required = true)
    @Override
    public ServiceResult<IndividualFaceDTO> getIndividualFace(GetIndividualFaceReq req) {
        ServiceResult<IndividualFaceDTO> serviceResult = new ServiceResult<>();
        IndividualFaceDTO result = null;
        EsignLog esignLog = mapper.map(req, EsignLog.class);
        esignLog.setCreateBy(req.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_21);
        esignLog.setRequestMsg(req.toString());
        try {
            result = esignApiModel.getIndividualFace(req);
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            esignLog.setResponseMsg(result.toString());
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(serviceResult.getErrorCode() + serviceResult.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        }
        serviceResult.setResult(result);
        esignLogMapper.insert(esignLog);
        return serviceResult;
    }

    /* add by 2019年11月7日, PM 06:29:51 DaiGaoLe begin */
    @POST
    @Path("persons-three")
    @ApiOperation(value = "个人运营商3要素信息比对",
        notes = "个人运营商3要素信息比对,以公安、移动网络运营商、银行、企业登记机构等数据源的预留信息作为基础，校验采集的自然人或者组织机构的信息真实有效")
    @Override
    public ServiceResult<String> personsServiceProviderThreeFactorVerification(PersonsThreeFactorReq requestObject) {
        ServiceResult<String> serviceResult = new ServiceResult<>();
        String result = null;
        EsignLog esignLog = mapper.map(requestObject, EsignLog.class);
        esignLog.setCreateBy(requestObject.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_15);
        esignLog.setRequestMsg(requestObject.toString());
        try {
            result = esignApiModel.personsServiceProviderThreeFactorVerification(requestObject);
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            esignLog.setResponseMsg(result.toString());
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(serviceResult.getErrorCode() + serviceResult.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        }
        serviceResult.setResult(result);
        esignLogMapper.insert(esignLog);
        return serviceResult;
    }

    @POST
    @Path("bank4-factors")
    @ApiOperation(value = "个人银行卡4要素信息比对",
        notes = "个人银行卡4要素信息比对，账号、户名、手机号、身份证号,以公安、移动网络运营商、银行、企业登记机构等数据源的预留信息作为基础，校验采集的自然人或者组织机构的信息真实有效")
    @Override
    public ServiceResult<String> bank4FactorsVerification(Bank4FactorsReq requestObject) {
        ServiceResult<String> serviceResult = new ServiceResult<>();
        String subbranchDto = null;
        EsignLog esignLog = mapper.map(requestObject, EsignLog.class);
        esignLog.setCreateBy(requestObject.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_10);
        esignLog.setRequestMsg(requestObject.toString());
        try {
            subbranchDto = esignApiModel.bank4FactorsVerification(requestObject);
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(serviceResult.getErrorCode() + serviceResult.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        }
        serviceResult.setResult(subbranchDto);
        esignLog.setResponseMsg(serviceResult.getResult().toString());
        esignLogMapper.insert(esignLog);
        return serviceResult;
    }

    @POST
    @Path("bureau4-factors")
    @ApiOperation(value = "企业4要素信息比对",
        notes = "企业4要素信息比对，企业名称、统一社会信用代码、法人名称、法人身份证号,以公安、移动网络运营商、银行、企业登记机构等数据源的预留信息作为基础，校验采集的自然人或者组织机构的信息真实有效")
    @Override
    public ServiceResult<String> bureau4FactorsVerification(Bureau4FactorsReq requestObject) {
        ServiceResult<String> serviceResult = new ServiceResult<>();
        String subbranchDto = null;
        EsignLog esignLog = mapper.map(requestObject, EsignLog.class);
        esignLog.setCreateBy(requestObject.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_14);
        esignLog.setRequestMsg(requestObject.toString());
        try {
            subbranchDto = esignApiModel.bureau4FactorsVerification(requestObject);
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(serviceResult.getErrorCode() + serviceResult.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        }
        serviceResult.setResult(subbranchDto);
        esignLog.setResponseMsg(serviceResult.getResult().toString());
        esignLogMapper.insert(esignLog);
        return serviceResult;
    }

    @POST
    @Path("bureau2-factors")
    @ApiOperation(value = "企业2要素信息比对",
        notes = "企业2要素信息比对，企业名称、统一社会信用代码,以公安、移动网络运营商、银行、企业登记机构等数据源的预留信息作为基础，校验采集的自然人或者组织机构的信息真实有效")
    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public ServiceResult<String> bureau2FactorsVerification(Bureau2FactorsReq requestObject) {
        ServiceResult<String> serviceResult = new ServiceResult<>();
        String subbranchDto = null;
        EsignLog esignLog = mapper.map(requestObject, EsignLog.class);
        esignLog.setCreateBy(requestObject.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_20);
        esignLog.setRequestMsg(requestObject.toString());
        try {
            subbranchDto = esignApiModel.bureau2FactorsVerification(requestObject);
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(serviceResult.getErrorCode() + serviceResult.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        }
        serviceResult.setResult(subbranchDto);
        esignLog.setResponseMsg(serviceResult.getResult().toString());
        esignLogMapper.insert(esignLog);
        return serviceResult;
    }

    @POST
    @Path("enterprise-fourFactors")
    @ApiOperation(value = "发起企业实名认证4要素校验",
        notes = "发起企业实名认证4要素校验<br/>1.调用查询打款银行信息接口<br/>2.调用发起随机金额打款认证<br/>3.调用随机打款金额校验")
    @ApiImplicitParam(name = "requestObj", dataType = "EnterpriseCertification4FactorVerificationReq",
        paramType = "body", required = true)
    @Override
    public ServiceResult<String>
        enterpriseCertificationFourFactorVerification(EnterpriseCertification4FactorVerificationReq requestObject) {
        ServiceResult<String> serviceResult = new ServiceResult<>();
        String result = null;
        EsignLog esignLog = mapper.map(requestObject, EsignLog.class);
        esignLog.setCreateBy(requestObject.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_16);
        esignLog.setRequestMsg(requestObject.toString());
        try {
            result = esignApiModel.enterpriseCertificationFourFactorVerification(requestObject);
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(serviceResult.getErrorCode() + serviceResult.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        }
        serviceResult.setResult(result);
        esignLog.setResponseMsg(serviceResult.getResult().toString());
        esignLogMapper.insert(esignLog);
        return serviceResult;
    }

    @POST
    @Path("transfer-random")
    @ApiOperation(value = "发起随机金额打款认证", notes = "发起随机金额打款认证")
    @ApiImplicitParam(name = "requestObj", dataType = "EnterpriseCertification4FactorVerificationReq",
        paramType = "body", required = true)
    @Override
    public ServiceResult<String> transferRandomAmount(TransferRandomAmountReq requestObject) {
        ServiceResult<String> serviceResult = new ServiceResult<>();
        String result = null;
        EsignLog esignLog = mapper.map(requestObject, EsignLog.class);
        esignLog.setCreateBy(requestObject.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_11);
        esignLog.setRequestMsg(requestObject.toString());
        try {
            result = esignApiModel.transferRandomAmount(requestObject);
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(serviceResult.getErrorCode() + serviceResult.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        }
        serviceResult.setResult(result);
        esignLog.setResponseMsg(serviceResult.getResult().toString());
        esignLogMapper.insert(esignLog);
        return serviceResult;
    }

    @POST
    @Path("verify-random-amount")
    @ApiOperation(value = "随机打款金额校验", notes = "随机打款金额校验")
    @ApiImplicitParam(name = "requestObj", dataType = "VerifyRandomAmountReq", paramType = "body", required = true)
    @Override
    public ServiceResult<String> verifyRandomAmountReq(VerifyRandomAmountReq requestObject) {
        ServiceResult<String> serviceResult = new ServiceResult<>();
        String result = null;
        EsignLog esignLog = mapper.map(requestObject, EsignLog.class);
        esignLog.setCreateBy(requestObject.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_12);
        esignLog.setRequestMsg(requestObject.toString());
        try {
            result = esignApiModel.verifyRandomAmountReq(requestObject);
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(serviceResult.getErrorCode() + serviceResult.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        }
        serviceResult.setResult(result);
        esignLog.setResponseMsg(serviceResult.getResult().toString());
        esignLogMapper.insert(esignLog);
        return serviceResult;
    }

    @POST
    @Path("subbranch")
    @ApiOperation(value = "查询打款银行信息", notes = "查询打款银行信息")
    @ApiImplicitParam(name = "requestObj", dataType = "SubbranchReq", paramType = "body", required = true)
    @Override
    public ServiceResult<List<SubbranchDto>> subbranch(SubbranchReq requestObject) {
        ServiceResult<List<SubbranchDto>> serviceResult = new ServiceResult<>();
        List<SubbranchDto> result = null;
        EsignLog esignLog = mapper.map(requestObject, EsignLog.class);
        esignLog.setCreateBy(requestObject.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_07);
        esignLog.setRequestMsg(requestObject.toString());
        try {
            result = esignApiModel.subbranch(requestObject);
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
        } catch (BusinessException be) {
            serviceResult.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        } catch (Exception e) {
            // 获取当前方法名
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            serviceResult.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(serviceResult.getErrorCode() + serviceResult.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return serviceResult;
        }
        serviceResult.setResult(result);
        esignLog.setResponseMsg(serviceResult.getResult().toString());
        esignLogMapper.insert(esignLog);
        return serviceResult;
    }
    /* add by 2019年11月7日, PM 06:29:51 DaiGaoLe end */

    /**
     * 发起运营商3要素核身认证
     *
     * @param reqObj 运营商3要素核身认证请求对象
     * @return 认证flowIa
     */
    @POST
    @Path("valid-telecom3")
    @ApiOperation(value = "发起运营商3要素核身认证", notes = "1.对个人运营商三要素信息进行核验，成功后向手机号发送验证码,并返回一个认证流程Id<br/>2.调用手机号验证码校验")
    @ApiImplicitParam(name = "requestObj", dataType = "ValidPersonTelecom3Req", paramType = "body", required = true)
    @Override
    public ServiceResult<String> validTelecom3Factors(ValidPersonTelecom3Req reqObj) {
        ServiceResult<String> result = new ServiceResult<>();
        EsignLog esignLog = mapper.map(reqObj, EsignLog.class);
        esignLog.setCreateBy(reqObj.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_09);
        esignLog.setRequestMsg(reqObj.toString());
        try {
            result.setResult(esignApiModel.validTelecom3Factors(reqObj));
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            esignLog.setResponseMsg(result.getResult().toString());
            esignLogMapper.insert(esignLog);
            return result;
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(result.getErrorCode() + result.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return result;
        }
        esignLogMapper.insert(esignLog);
        return result;
    }

    /**
     * 发起银行卡4要素核身认证
     *
     * @param reqObj 银行卡4要素核身认证请求对象
     * @return 认证flowId
     * @author liulx
     */
    @POST
    @Path("valid-bank-card4")
    @ApiOperation(value = "发起银行卡4要素核身认证", notes = "1.对个人银行卡四要素信息进行核验，成功后向手机号发送验证码,并返回一个认证流程Id<br/>2.调用银行预留手机号验证码校验")
    @ApiImplicitParam(name = "requestObj", dataType = "ValidPersonBankCard4Req", paramType = "body", required = true)
    @Override
    public ServiceResult<String> validBankCard4Factors(ValidPersonBankCard4Req reqObj) {
        ServiceResult<String> result = new ServiceResult<>();
        EsignLog esignLog = mapper.map(reqObj, EsignLog.class);
        esignLog.setCreateBy(reqObj.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_13);
        esignLog.setRequestMsg(reqObj.toString());
        try {
            result.setResult(esignApiModel.validBankCard4Factors(reqObj));
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            esignLog.setResponseMsg(result.getResult().toString());
            esignLogMapper.insert(esignLog);
            return result;
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(result.getErrorCode() + result.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return result;
        }
        esignLogMapper.insert(esignLog);
        return result;
    }

    /**
     * 查询认证信息
     *
     * @param reqObj 查询认证信息请求对象
     * @return 认证flowId
     * @author liulx
     */
    @POST
    @Path("valid-detail")
    @ApiOperation(value = "查询认证信息", notes = "查询实名认证流程的流程详情，含认证状态、认证主体信息等")
    @ApiImplicitParam(name = "requestObj", dataType = "QueryEsignValidDetailReq", paramType = "body", required = true)
    @Override
    public ServiceResult<EsignValidDetailDTO> getValidDetail(QueryEsignValidDetailReq reqObj) {
        ServiceResult<EsignValidDetailDTO> result = new ServiceResult<>();
        EsignLog esignLog = mapper.map(reqObj, EsignLog.class);
        esignLog.setCreateBy(reqObj.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_08);
        esignLog.setRequestMsg(reqObj.toString());
        try {
            result.setResult(esignApiModel.getValidDetail(reqObj));
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            esignLog.setResponseMsg(result.getResult().toString());
            esignLogMapper.insert(esignLog);
            return result;
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(result.getErrorCode() + result.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return result;
        }
        esignLogMapper.insert(esignLog);
        return result;
    }

    /**
     * 运营商三要素短信验证码校验
     *
     * @param req e签宝手机验证码校验请求对象
     * @return 校验结果 success 字段值标识 true/false
     */
    @PUT
    @Path("check-code-phone")
    @ApiOperation(value = "手机号验证码校验", notes = "回填校验运营商三要素短信验证码")
    @ApiImplicitParam(name = "requestObj", dataType = "CheckEsignVerificationCodeReq", paramType = "body",
        required = true)
    @Override
    public ServiceResult checkCodeByTelecom3Factors(CheckEsignVerificationCodeReq req) {
        ServiceResult result = new ServiceResult<>();
        EsignLog esignLog = mapper.map(req, EsignLog.class);
        esignLog.setCreateBy(req.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_18);
        esignLog.setRequestMsg(req.toString());
        try {
            esignApiModel.checkVerificationCode(req, 0);
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            esignLogMapper.insert(esignLog);
            return result;
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(result.getErrorCode() + result.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return result;
        }
        esignLogMapper.insert(esignLog);
        return result;
    }

    /**
     * 银行预留手机号验证码校验
     *
     * @param req e签宝手机验证码校验请求对象
     * @return 校验结果 success 字段值标识 true/false
     */
    @PUT
    @Path("check-code-bank-phone")
    @ApiOperation(value = "银行预留手机号验证码校验", notes = "校验银行四要素短信验证码")
    @ApiImplicitParam(name = "requestObj", dataType = "CheckEsignVerificationCodeReq", paramType = "body",
        required = true)
    @Override
    public ServiceResult checkCodeByBankCard4Factors(CheckEsignVerificationCodeReq req) {
        ServiceResult result = new ServiceResult<>();
        EsignLog esignLog = mapper.map(req, EsignLog.class);
        esignLog.setCreateBy(req.getOperationUserId());
        esignLog.setGmtCreate(LocalDateTime.now());
        esignLog.setOperationType(SystemConstant.ESIGN_OPERATION_17);
        esignLog.setRequestMsg(req.toString());
        try {
            esignApiModel.checkVerificationCode(req, 1);
            esignLog.setStatus(SystemConstant.INVOKING_SUCCESS_STATUS);
            esignLogMapper.insert(esignLog);
            return result;
        } catch (BusinessException be) {
            esignLog.setResponseMsg(be.getCode() + be.getMessage());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            esignLog.setResponseMsg(result.getErrorCode() + result.getErrorMsg());
            esignLog.setStatus(SystemConstant.INVOKING_FAIL_STATUS);
            esignLogMapper.insert(esignLog);
            return result;
        }
        esignLogMapper.insert(esignLog);
        return result;
    }

    /**
     * 获取中台e签宝token
     *
     * @param req 获取中台e签宝token
     * @return 中台e签宝token
     */
    @POST
    @Path("get-esign-token")
    @ApiOperation(value = "获取中台e签宝token,当与中台使用相同的e签宝账号信息并且不通过中台对接e签宝时使用",
        notes = "获取中台e签宝token,当与中台使用相同的e签宝账号信息并且不通过中台对接e签宝时使用")
    @ApiImplicitParam(name = "requestObj", dataType = "BaseReqObj", paramType = "body", required = true)
    @Override
    public ServiceResult getEsignToken(BaseReqObj req) {
        ServiceResult result = new ServiceResult<>();
        try {
            result.setResult(apiHelper.getAccessToken());
            return result;
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "case: {}",
                Throwables.getStackTraceAsString(e));
            result.setError(ErrorMessageConstant.ECUC0000_CODE, ErrorMessageConstant.ECUC0000_MESSAGE);
            return result;
        }
        return result;
    }

}
